<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="民族代码" prop="code">
        <el-input v-model="dataForm.code" placeholder="民族代码"></el-input>
      </el-form-item>
      <el-form-item label="民族名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="民族名称" @change="genPinYinCode"></el-input>
      </el-form-item>
      <el-form-item label="拼音码" prop="pinyinCode">
        <el-input v-model="dataForm.pinyinCode" placeholder="拼音码"></el-input>
      </el-form-item>
      <el-form-item label="分布区域" prop="area">
        <el-input v-model="dataForm.area" placeholder="分布区域"></el-input>
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input v-model="dataForm.description" placeholder="描述"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-prevent-re-click>确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import {
    getObj,
    addObj,
    putObj
  } from '@/api/admin/national'
  import {
    genFirstPinYinCode
  } from "@/util/util";

  export default {
    data() {
      return {
        visible: false,
        dataForm: {
          id: 0,
          code: '',
          name: '',
          pinyinCode: '',
          area: '',
          description: ''
        },
        dataRule: {
          code: [{
            required: true,
            message: '民族代码不能为空',
            trigger: 'blur'
          }],
          name: [{
            required: true,
            message: '民族名称不能为空',
            trigger: 'blur'
          }],
          pinyinCode: [{
            required: true,
            message: '拼音码不能为空',
            trigger: 'blur'
          }]
        }
      }
    },
    methods: {
      init(id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getObj(this.dataForm.id).then(response => {
              this.dataForm = response.data.data
            })
          }
        })
      },
      genPinYinCode() {
        let pinYinCode = genFirstPinYinCode(this.dataForm.name)
        if (!pinYinCode) return '';
        pinYinCode = pinYinCode.toString();
        this.dataForm.pinyinCode = pinYinCode.toUpperCase()
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
              putObj(this.dataForm).then(data => {
                this.$message.success('修改成功')
                this.visible = false
                this.$emit('refreshDataList')
              });
            } else {
              addObj(this.dataForm).then(data => {
                this.$message.success('添加成功')
                this.visible = false
                this.$emit('refreshDataList')
              })
            }
          }
        })
      }
    }
  }
</script>
